var app = getApp(), fetchApi = require("../../../utils/httpPost.js"), 
i = require("../../../utils/arrayUtil.js");
var common_Js = require("../../../component/common.js")
Page({
  /**
   * 页面的初始数据
   */
  data: {
    hasData:false,
    loading: false,
    netWork: false,
    isLoading: true,
    goodsList:[],
    pageSize:10,
    pageIndex:1,
    mer_id:0,
    mer_no:'',
    keyword:'',
    mapName:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    if(options.q) {
      var q = decodeURIComponent(options.q) // 获取到二维码原始链接内容
      // const scancode_time = parseInt(options.scancode_time) // 获取用户扫码时间 UNIX 时间戳
      q = "?" + q.split("?")[1];
      this.data.query = q;
      console.log('q', q)
      options = common_Js.getQueryVariable(q);
      console.log('options',options)
      if(options.id){
        var id = parseInt(options.id) 
        that.data.mer_id = id;
      }
      if(options.no){
        that.data.mer_no = options.no;
      }
    }
    if(options.mer_id){
      this.data.mer_id = parseInt(options.mer_id)
      // this.onHttpInit()
    }
    if(options.no){
      that.data.mer_no = options.no;
    }
    this.onHttpInit()
  },  
  onSearch(val){
    console.log('onSearch',val)
    this.data.keyword = val.detail;
    this.data.pageIndex = 1;
    this.setData({
      isLoading: true,
      goodsList:[],
    })
    this.onHttpInit()
  },
  onShow:function(){
  },
  onHttpInit: function () {
    var that = this
    fetchApi.httpPost("/product/list/new", {
      mer_id: that.data.mer_id,
      mer_no: that.data.mer_no,
      pageSize:that.data.pageSize,
      pageIndex: that.data.pageIndex,
      is_mer:1,
      keyword: that.data.keyword,
    }, function (res) {
      console.log("获取商品列表返回数据:", res)
      if (res.data.code == 0) {
        if (res.data.totalcount > 0) {
          that.data.pageIndex = that.data.pageIndex + 1
          console.log("商家商品列表数据", res)
          var products = res.data.data.productList;
          let mapName = res.data.data.mapName;
          if(mapName!=undefined){
            wx.setNavigationBarTitle({
              title: mapName,
            })
          }
          if(products.length > 0){
            for (var index in products) {
              that.data.goodsList.splice(that.data.goodsList.length, 0, products[index]);
            }
            that.setData({
              hasData: true,
              goodsList: that.data.goodsList
            })
          }
        }
      }
      that.setData({
         isLoading: false, loading: true, netWork: true,
      })
    }, function (a) {
      console.log("获取商品列表返回数据 err:", a);
      that.setData({ hasData: false,  isLoading: false,  loading: true, netWork: true })
    });
  },
  goAddProduct(e){
    wx.navigateTo({
      url: '/pagesMer/pages/product/add/index?mer_id=' + this.data.mer_id + '&product_id=0',
    })
  },
  goEdit(e){
    console.log('goEdit',e);
    var productId = e.detail.currentTarget.dataset.item.id;
    wx.navigateTo({
      url: '/pagesMer/pages/product/add/index?mer_id=' + this.data.mer_id + '&product_id=' + productId,
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.data.pageIndex = 1
    this.setData({
      goodsList:[],
      isLoading:true,
    })
    this.onHttpInit();
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.setData({
      goodsList:[],
      isLoading:true,
    })
    this.onHttpInit();
  },
})